//
//  HMSportTrackingLine.m
//  BlackHorseLine
//
//  Created by made on 17/3/5.
//  Copyright © 2017年 made. All rights reserved.
//

#import "HMSportTrackingLine.h"
#import "HMSportPolyLine.h"

@interface HMSportTrackingLine ()

//起始点位置
@property(nonatomic,strong)CLLocation *startLocation;
//结束点位置
@property(nonatomic,strong)CLLocation *endLocation;

@end


@implementation HMSportTrackingLine
- (instancetype)initWithStartLocation:(CLLocation *)startLocation EndLocation:(CLLocation *)endLocation
{
    self = [super init];
    self.startLocation = startLocation;
    self.endLocation = endLocation;
    return self;
}


- (HMSportPolyLine *)polyline
{
    //构造折线数据对象
    CLLocationCoordinate2D commonPolylineCoords[2];
    
    //折线起点
    commonPolylineCoords
    [0].latitude = self.startLocation.coordinate.latitude;
    commonPolylineCoords[0].longitude = self.startLocation.coordinate.longitude;
    
    commonPolylineCoords[1].latitude = self.endLocation. coordinate.latitude;
    commonPolylineCoords[1].longitude = self.endLocation. coordinate.longitude;
    
    //根据不同的速度显示不同的颜色(实际开发中根据产品经理)
    //由于速度值比较小颜色不明显,这里暂时定义一个放大因子
    float factor = 8;
    float red = self.speed * factor;
    
    //NSLog(@"%f km/h",self.speed);
    
    UIColor *color = [UIColor colorWithRed:red/255 green:1 blue:0 alpha:1];
    
    //构造折线对象(是一个model)
    HMSportPolyLine *commonPolyline = [[HMSportPolyLine alloc]polylingWithCoordinates:commonPolylineCoords count:2 Color:color];
    return commonPolyline;
    
}

- (double)speed
{
    //1.线条的平均速度=(起始点的瞬间速度+结束点的瞬间速度)/2
    double speed = (self.startLocation.speed + self.endLocation.speed) * 0.5;
    //2.速度单位转换 系统默认情况下速度单位是m/s转化为km/h()
    return speed * 3.6;
}

-(double)time
{
    //线条终点位置时间戳与起点位置时间戳的差值就是该条轨迹的时间
    return [self.endLocation.timestamp timeIntervalSinceDate:self.startLocation.timestamp];
}

-(double)distance
{
    //距离(系统默认CLLocationDistance距离单位是m),转化为km * 0.001
    return [self.endLocation distanceFromLocation:self.startLocation] * 0.001;
}

@end
